#pragma once
#ifndef INPUT_HANDLER_H
#define INPUT_HANDLER_H

#include <graphics.h>
#include <vector>
#include <map>
#include <functional>

// Key state enumeration
enum class KeyState {
    Released,
    Pressed,
    JustPressed,
    JustReleased
};

// Mouse button enumeration
enum class MouseButton {
    Left,
    Right,
    Middle
};

// Input event types
enum class InputEventType {
    KeyDown,
    KeyUp,
    MouseMove,
    MouseDown,
    MouseUp,
    MouseWheel
};

// Input event structure
struct InputEvent {
    InputEventType type;
    union {
        int keyCode;        // Keyboard events
        struct {            // Mouse events
            int x, y;
            MouseButton button;
            int wheelDelta;
        } mouse;
    };
};

// Input handler class
class InputHandler {
private:
    // Key states
    std::map<int, KeyState> keyStates;
    std::map<int, KeyState> prevKeyStates;

    // Mouse states
    std::map<MouseButton, KeyState> mouseStates;
    std::map<MouseButton, KeyState> prevMouseStates;
    int mouseX, mouseY;
    int mouseWheelDelta;

    // Event queue
    std::vector<InputEvent> eventQueue;

    // Event callback functions
    std::map<InputEventType, std::vector<std::function<void(const InputEvent&)>>> eventCallbacks;

public:
    InputHandler();

    // Update input states
    void update();

    // Check key states
    bool isKeyDown(int keyCode) const;
    bool isKeyUp(int keyCode) const;
    bool wasKeyPressed(int keyCode) const;
    bool wasKeyReleased(int keyCode) const;

    // Check mouse states
    bool isMouseButtonDown(MouseButton button) const;
    bool isMouseButtonUp(MouseButton button) const;
    bool wasMouseButtonPressed(MouseButton button) const;
    bool wasMouseButtonReleased(MouseButton button) const;

    // Get mouse position
    int getMouseX() const;
    int getMouseY() const;
    int getMouseWheelDelta() const;

    // Register event callbacks
    void registerCallback(InputEventType type, const std::function<void(const InputEvent&)>& callback);

    // Clear event queue
    void clearEvents();
};

#endif // INPUT_HANDLER_H  